{% extends "layouts/layout.html" %}
{% block title %}
View Item
{% endblock %}
{% block content %}
<script type="text/javascript" src="/js/items/items.js"></script>
<h3>View Item</h3>
{% if user %}
<h5>Rate this seller: <a href="/users/list_user_feedback?user_id={{item.created_by_id}}">List of feedback</a></h5>
<div id="seller_rating"></div>
<script type="text/javascript">
$('#seller_rating').raty({
	score : {% if rating %}{{ rating }}{% else %}3{% endif %} ,
	{% if rating %}
	readOnly:true
	{% else %}
	click: function(score, evt) {
		if(score > 5){
			score = 5;
		}else if(score < 0){
			score = 0;
		}
		var redir = $('#redirect').text() + score;
		if(confirm('Are you sure you want to give a rating of ' + score + ' to this seller?'))
		window.location = redir;
	}
	{% endif %}
});
</script>
<div id="redirect" class="hide">/users/submit_feedback?for_user_id={{item.created_by_id}}&amp;xsrf_token={{xsrf_token}}&amp;rating=</div><br/>
{% if item.bidding_enabled %}
<div class="well">
	<p>
		{% if item.highest_bid %}
		{% if not(item.sold) %}
		<h4>
			The highest bid right now is: ${{item.highest_bid}}
			{% if item.created_by_id == user.user_id() %}
			| <a href="/items/item_sold?item_id={{item.key().id()}}&xsrf_token={{xsrf_token}}">Click here if you would like to mark this item as sold.</a>
			{%endif%}
		</h4>
		{% endif %}
		{% else %}
		<h4>There are no bids on this item yet.</h4>
		{% endif %}
		{% if item.sold %}
		<h4>This item was bought by {{buyer.nickname}} for ${{item.highest_bid}}.</h4>
		{% else %}
		<h5>Place your bid (the asking price is: ${{item.price}}):</h5>
		<form action="/items/submit_bid" id="bid_form" method="post">
			<input type="hidden" name="item_id" value="{{item.key().id()}}"/>
			<input type="hidden" name="xsrf_token" value="{{xsrf_token}}"/>
			<p>
				Bid:<br />
				<div class="input-prepend">
					<span class="add-on">$</span>
					<input name="bid" id="bid" type="text">
				</div>
			</p>
			<button class="btn btn-primary" type="submit" value="Save">Submit Bid</button>
		</form>
		{% endif %}
	</p>
</div>
{% endif %}
{% endif %}
<div class="well">
	<p>
		Posted by: <a href='/users/shop?user_id={{ li.user_id }}'>{{ li.get_display_name() }}</a>
		{% if item.deactivated %}
		&lt<i>Deactivated</i>&gt
		{% endif %}
	</p>
	<p>Seller rating: <strong>{% if li.get_average_rating() %}{{ li.get_average_rating() }} / 5.00 {% else %} None {% endif %}</strong></p>
	<p>Title: {{item.title}}</p>
	<p>Description: <textarea class="readonlyRichText">{{item.description}}</textarea></p>
	<p>Price: ${{item.price}}</p>
	<p>Created on: {{item.created_at.strftime("%m/%d/%Y at %H:%M")}}</p>
	<p>{{ item.display_image() }}</p>
</div>
<p>
	{% if current_li %}<a href="/threads/new_thread?about={{item.key().id()}}" class="btn">Contact Seller</a>{% endif %}
	<a href="/items/" class="btn">Back</a>
</p>
{% if user %}
<p>
	<form action="/items/submit_feedback" method="post">
		<input type="hidden" name="item_id" value="{{item.key().id()}}"/>
		<input type="hidden" name="xsrf_token" value="{{xsrf_token}}"/>
    <input type="hidden" name="rating" id="rating" value="3" />
		<div class="well">
			<h4>Are you the buyer? Leave your feedback here:</h4>
			<table>
				<tr>
					<td><textarea name="feedback"></textarea></td>
					<td>Rating:</td>
					<td><div id="rating_div"></div></td>
				</tr>
			</table>
			<button type="submit" class="btn" value="submit">Submit feedback</button>
		</div>
	</form>
</p>
{% endif %}
{% if feedback.count() > 0 %}
<p>
	<h3>Here's what people had to say about this item:</h3>
	{% for item_feedback in feedback %}
	<div class="well">
		{% if is_admin %}
		<a href="/items/delete_item_feedback?created_by={{item_feedback.parent().key().id()}}&feedback_id={{item_feedback.key().id()}}&xsrf_token={{xsrf_token}}">
			<button class="btn btn-primary"  value="Delete">Delete Feedback</button>
		</a>
		{% endif %}
		<h6>{{item_feedback.parent().nickname}} wrote:</h6>
		<p>{{item_feedback.feedback}}</p>
		<h6>And left a rating of:</h6>
		<p><div class="raty_rating" data-rating="{{item_feedback.rating}}"></div></p>
	</div>
	{% endfor %}
</p>
{% endif %}
{% endblock %}